//
// Created by 11510 on 2024/6/17.
//

#ifndef DEMO1_PERSON_H
#define DEMO1_PERSON_H
#include <stdio.h>
#include <cstring>

class Person {
private:
    char* name;
    int age;
public:
    Person():name(new char[1]()),age(0){}

    Person(const char* name, int age):name(new char[strlen(name) + 1]()), age(age){
        strcpy(this->name, name);
    }

    Person(const Person& rhs):name(new char[strlen(rhs.name) + 1]()), age(rhs.age){
//        printf("copy Person()\n");
        strcpy(name, rhs.name);
    }

    Person& operator=(const Person& rhs){
//        printf("operator=\n");
        if(&rhs != this){
            delete[] name;
            name = new char[strlen(rhs.name) + 1]();
            strcpy(name, rhs.name);

            age = rhs.age;
        }
        return *this;
    }

    ~Person(){
//        printf("~Person()\n");
        if(name != nullptr){
            delete[] name;
            name = nullptr;
        }
    }
    void display()const{
        printf("name = %s, age = %d\n", name, age);
    }
};


#endif //DEMO1_PERSON_H
